import { useContext, useEffect, useState } from "react";
import ReduxContext from "./context";

export const connect =
  (mapStateToProps, mapDispatchToProps) => (MyComponent) => {
    return function (props) {
      const _store = useContext(ReduxContext);
      const [Bool, setBool] = useState(true);
      const forceUpdate = () => setBool((b) => !b);
      useEffect(() => {
        _store.subscribe(forceUpdate);
      }, []);

      return (
        <ReduxContext.Consumer>
          {(store) => (
            <MyComponent
              {...props}
              {...mapStateToProps(store.getState())}
              {...mapDispatchToProps(store.dispatch)}
            />
          )}
        </ReduxContext.Consumer>
      );
    };
  };
